package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.itheima.pojo.Dept;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;
    @GetMapping
    public Result list(){
        log.info("查询全部数据");
        List<Dept> DeptList=deptService.list();
        return Result.success(DeptList);
    }
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) throws Exception {
        log.info("根据id删除部门:{}",id);
        deptService.delete(id);
        return Result.success();
    }
    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept1){
        log.info("根据部门名称添加部门:{}",dept1);
        deptService.add(dept1);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result select(@PathVariable Integer id){
        log.info("根据id查询部门：{}",id);
        List<Dept> deptList=deptService.select(id);
        return Result.success(deptList);
    }
    @Log
    @PutMapping
    public Result update(@RequestBody Dept dept2){
        log.info("根据部门名称修改部门信息:{}",dept2);
        deptService.update(dept2);
        return Result.success();
    }
}

